//
//  EquipmentViewController.m
//  GameBoss
//
//  Created by 张艺 on 15/4/30.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "EquipmentViewController.h"
#import "Brands.h"
#import "Sections.h"
#import "BrandsMore.h"
#import "RecommondBrands.h"
#import "TotalBrands.h"
#import "ProductViewController.h"
#import "BrandViewController.h"

@interface EquipmentViewController ()
// 显示分类的tableView
@property (nonatomic, retain) UITableView *tableView;

// id数组
@property (nonatomic, retain) NSMutableArray *classIdArr;
@property (nonatomic, retain) NSMutableArray *DIYIdArr;
@property (nonatomic, retain) NSMutableArray *HardwareIdArr;

// 装分类的数组
@property (nonatomic, retain) NSMutableArray *classArray;

// 显示不同商家的tableview
@property (nonatomic, retain) UITableView *brandTableView;

// 显示DIY硬件的内容数组
@property (nonatomic, retain) NSMutableArray *DIYArray;
// 显示硬件外设的内容数组
@property (nonatomic, retain) NSMutableArray *HardwareArray;


// 空数组  为了给第二个tableview赋值
@property (nonatomic, retain) NSMutableArray *tempArr;
@property (nonatomic, retain) NSMutableArray *brandArr;
@property (nonatomic, retain) NSMutableArray *secArr;
@property (nonatomic, retain) NSMutableArray *brandMoreArr;
// 字典
@property (nonatomic, retain) NSDictionary *recDic;
//存储要传入后方的第一个参数
@property (nonatomic, copy) NSString *type;

@property (nonatomic, retain) NSDictionary *totDic;
//title数组
@property (nonatomic, retain) NSMutableArray *titleArr;

@property(nonatomic, assign)NSInteger flag;

@end

@implementation EquipmentViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"设备";
        self.classArray = [NSMutableArray arrayWithObjects:@"手机", @"笔记本", @"DIY硬件", @"超极本", @"平板电脑", @"硬件外设", nil];
        self.DIYArray = [NSMutableArray arrayWithObjects:@"CPU", @"显卡", @"主板", @"内存", @"硬盘", @"固态硬盘SSD", @"显示器", @"光驱/刻录机", @"声卡", @"PC电源", nil];
        self.HardwareArray = [NSMutableArray arrayWithObjects:@"键盘", @"鼠标", @"音箱", @"摄像头", @"鼠标垫", @"硬盘/光驱盒", @"UPS电源", @"UPS电池", @"单机多用户/拖", @"硬盘保护卡", @"拷贝机", nil];
        self.classIdArr = [NSMutableArray arrayWithObjects:@"20937", @"20807", @"95585", @"79849", nil];
        self.DIYIdArr = [NSMutableArray arrayWithObjects:@"20812", @"20817", @"20811", @"20813", @"20814", @"42997", @"20950", @"20874", @"20818", @"20881", nil];
        self.HardwareIdArr = [NSMutableArray arrayWithObjects:@"20873", @"20820", @"20848", @"20946", @"25521", @"33069", @"20864", @"35290", @"26511", @"21004", @"21006", nil];
        
        self.tempArr = [NSMutableArray array];
        self.brandArr = [NSMutableArray array];
        self.secArr = [NSMutableArray array];
        self.brandMoreArr = [NSMutableArray array];
        self.recDic = [NSDictionary dictionary];
        self.titleArr = [NSMutableArray array];
        self.totDic = [NSDictionary dictionary];
    }

    return self;
}

-(void)equipmentData:(NSString *)idStr{
    
    NSString *strURL = [NSString stringWithFormat:@"http://mrobot.pconline.com.cn/v3/product/brand/%@", idStr];
    [AFN_Get netWorkingTool:strURL block:^(id responseObject) {
        
        NSDictionary *parDic = responseObject[@"partition"];
        self.type = responseObject[@"type"];
        NSDictionary *recDic = parDic[@"recommondBrands"];
        NSMutableArray *braTempArr = recDic[@"brands"];
        self.brandArr = [NSMutableArray array];
#pragma mark 第一个brands
        for (NSDictionary *temp in braTempArr) {
            Brands *braModel = [[Brands alloc] init];
            // setValuesForKeysWithDictionary 有时会出现问题
//            [braModel setValuesForKeysWithDictionary:temp];
            //需要单独传值
            //有时会出现问题
            braModel.name = temp[@"name"];
            braModel.logo = temp[@"logo"];
            braModel.brandsId = temp[@"id"];
            [self.brandArr addObject:braModel];
            [braModel release];
        }
        self.totDic = [NSMutableDictionary dictionary];
        self.totDic = parDic[@"totalBrands"];
        NSArray *secTempArr = self.totDic[@"sections"];
        self.secArr = [NSMutableArray array];
#pragma mark 第二个 section 中的 brands
        for (int i = 0 ; i < secTempArr.count; i++) {
            Sections *secModel = [[Sections alloc] init];
            NSMutableDictionary *sectionsDic = secTempArr[i];
            [secModel setValuesForKeysWithDictionary:sectionsDic];
            secModel.brands = [NSMutableArray array];
            for (int j = 0; j < [sectionsDic[@"brands"] count]; j ++) {
                NSMutableDictionary *brandsDic =  sectionsDic[@"brands"][j];
                BrandsMore *brands = [[BrandsMore alloc] init];
                [brands setValuesForKeysWithDictionary:brandsDic];
                [secModel.brands addObject:brands];
            }
            [self.secArr addObject:secModel];
        }
        [self.brandTableView reloadData];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
#pragma mark 加入背景图
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Screen.width, Screen.height)];
    imageView.image = [UIImage imageNamed:@"table"];
    [self.view addSubview:imageView];
    
#pragma mark tableView用Grouped的style, title不会因为滚动,变化,  Plain会停留title,直到section全部结束,才结束
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen.width / 3, Screen.height) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    //设置tableView透明
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [_tableView release];
    
    
    self.brandTableView = [[UITableView alloc] initWithFrame:CGRectMake(Screen.width / 3, 0, Screen.width / 3 * 2, Screen.height-64-49) style:UITableViewStylePlain];
    [self.view addSubview:self.brandTableView];
    //设置tableView透明
    self.brandTableView.backgroundColor = [UIColor clearColor];
    self.brandTableView.delegate = self;
    self.brandTableView.dataSource = self;
    [_brandTableView release];
    
}

#pragma mark 第二个进行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.tableView) {
        return self.classArray.count;
    }
    if (tableView == self.brandTableView) {
        if ((self.flag == 2) || (self.flag == 5)) {
            return self.tempArr.count;
        }
        if ((self.flag == 0) || (self.flag == 1) || (self.flag == 3) || (self.flag == 4)){
            if (section == 0) {
                //返回brandArr(brands)数组的个数
                return self.brandArr.count;
            }else{
                for (int i=0; i<self.secArr.count; i++) {
                    if (section == i+1) {
//                        NSLog(@"%ld", [self.secArr[i] rowNum]);
                        return [self.secArr[i] rowNum];
                    }
                }
                //返回secArr数组中每个brands的数组的个数
                //第二次循环走这条,此时section变为1,所以数组应从(section - 1)开始遍历
                return [[self.secArr[section - 1] brands] count];

            }
        }
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView) {
        static NSString *reuse = @"reuse";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease];
        }
        cell.textLabel.text = self.classArray[indexPath.row];
        //设置cell透明
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.textColor = [UIColor whiteColor];
#pragma mark 设置点击颜色
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.contentView.frame];
        imageView.backgroundColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.5 alpha:0.6];
        cell.selectedBackgroundView = imageView;
        return cell;
    }
    if (tableView == self.brandTableView) {
        if ((self.flag == 2) || (self.flag == 5)) {
            static NSString *reuse = @"cell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
            if (!cell) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
            }
            cell.textLabel.text = self.tempArr[indexPath.row];
            //设置cell透明
            cell.backgroundColor = [UIColor clearColor];
            cell.textLabel.textColor = [UIColor whiteColor];
#pragma mark 设置点击颜色
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.contentView.frame];
            imageView.backgroundColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.5 alpha:0.6];
            cell.selectedBackgroundView = imageView;
            return cell;
        }
        if ((self.flag == 0) || (self.flag == 1) || (self.flag == 3) || (self.flag == 4)) {
            if (indexPath.section == 0) {
                static NSString *reuse2 = @"cell1";
                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse2];
                if (!cell) {
                    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse2] autorelease];
                }
                [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[self.brandArr[indexPath.row] logo]]];
                cell.detailTextLabel.text = [self.brandArr[indexPath.row] name];
                //设置cell透明
                cell.backgroundColor = [UIColor clearColor];
                cell.detailTextLabel.textColor = [UIColor whiteColor];
#pragma mark 设置点击颜色
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.contentView.frame];
                imageView.backgroundColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.5 alpha:0.6];
                cell.selectedBackgroundView = imageView;
                return cell;
            }else{
                static NSString *reuse3 = @"cell2";
                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse3];
                if (!cell) {
                    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse3] autorelease];
                }
#pragma mark section此时变为1了,数组要从0开始,所以section-1
                Sections *secModel =  self.secArr[indexPath.section - 1];
                BrandsMore *brandsMoreModel =  secModel.brands[indexPath.row];
                cell.textLabel.text = brandsMoreModel.name;
                //设置cell透明
                cell.backgroundColor = [UIColor clearColor];
                cell.textLabel.textColor = [UIColor whiteColor];
#pragma mark 设置点击颜色
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.contentView.frame];
                imageView.backgroundColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.5 alpha:0.6];
                cell.selectedBackgroundView = imageView;
                return cell;
            }
        }
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView) {
        [self.tempArr removeAllObjects];
        if (indexPath.row == 2) {
            for (int i = 0; i < self.DIYArray.count; i++) {
                [self.tempArr addObject:self.DIYArray[i]];
            }
            self.flag = 2;
            [self.brandTableView reloadData];
        }
        if (indexPath.row == 5) {
            for (int i = 0;  i < self.HardwareArray.count; i++) {
                [self.tempArr addObject:self.HardwareArray[i]];
            }
            self.flag = 5;
            [self.brandTableView reloadData];
        }
        if (indexPath.row == 0) {
            self.flag = 0;
            [self equipmentData:self.classIdArr[0]];
        }
        if (indexPath.row == 1){
            self.flag = 1;
            [self equipmentData:self.classIdArr[1]];
        }
        if (indexPath.row == 3){
            self.flag = 3;
            [self equipmentData:self.classIdArr[2]];
        }
        if (indexPath.row == 4){
            self.flag = 4;
            [self equipmentData:self.classIdArr[3]];
        }
    }
    else if(tableView == self.brandTableView)
    {
        if ((self.flag == 2) || (self.flag == 5)) {
            BrandViewController *brandVC = [[BrandViewController alloc] init];
            if(self.flag == 2){
                brandVC.idStrUrl = self.DIYIdArr[indexPath.row];
            }else if (self.flag == 5){
                brandVC.idStrUrl = self.HardwareIdArr[indexPath.row];
                NSLog(@"%@", brandVC.idStrUrl);
            }
            [self.navigationController pushViewController:brandVC animated:YES];
            [brandVC release];
        }
        if ((self.flag == 0) || (self.flag == 1) || (self.flag == 3) || (self.flag == 4)) {
            ProductViewController *proVC = [[ProductViewController alloc] init];
            if (indexPath.section == 0) {
                proVC.typeId = self.type;
                proVC.numID = [self.brandArr[indexPath.row] brandsId];
            }else{
                proVC.typeId = self.type;
                proVC.numID = [[self.secArr[indexPath.section - 1] brands][indexPath.row] id];
            }
            [self.navigationController pushViewController:proVC animated:YES];
            [proVC release];
        }
    }
}

#pragma mark 第一个进行
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (tableView == self.brandTableView) {
        if ((self.flag == 0) || (self.flag == 1) || (self.flag == 3) || (self.flag == 4)){
            
            return self.secArr.count + 1;
        }
    }
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (tableView == self.brandTableView) {
        if ((self.flag == 0) || (self.flag == 1) || (self.flag == 3) || (self.flag == 4)) {
            return Screen.height * 30/667;
        }
    }
    return 0;
}

#pragma mark 先进行
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

    self.titleArr = [NSMutableArray array];
    [self.titleArr addObject:@"荐"];
    for (int i=0; i<self.secArr.count; i++) {
        [self.titleArr addObject:[self.secArr[i] index]];
    }
    if (tableView == self.brandTableView) {
        if ((self.flag == 0) || (self.flag == 1) || (self.flag == 3) || (self.flag == 4)) {
            return self.titleArr;
        }
    }
    return nil;
}
#pragma mark 后进行
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    return self.titleArr[section];
}
#pragma mark 更改section的颜色
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
//    UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(Screen.width / 3, 0, Screen.width / 3 * 2, Screen.height * 30/667)];
//    customView.backgroundColor = [UIColor redColor];
//    return customView;
    UIView* customView = [[[UIView alloc] init] autorelease];
    customView.backgroundColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 90, 22)];
    titleLabel.textColor=[UIColor whiteColor];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.text=[self.titleArr objectAtIndex:section];
    [customView addSubview:titleLabel];
    [titleLabel release];
    return customView;
}
- (void)dealloc
{
    [_tableView release];
    [_classIdArr release];
    [_DIYIdArr release];
    [_HardwareIdArr release];
    [_classArray release];
    [_brandTableView release];
    [_DIYArray release];
    [_HardwareArray release];
    [_tempArr release];
    [_brandArr release];
    [_secArr release];
    [_brandMoreArr release];
    [_recDic release];
    [_totDic release];
    [_titleArr release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
